#include "num_str.h"

void str2int32(int *add,unsigned char str[])
{
	uint8_t i = 0;
	int temp = 0;
	if(str[0]=='-')
		i = 1;
	for(;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	*add = temp;
	if(str[0]=='-')
		temp=-temp;
}
void str2uint32(uint32_t *add,unsigned char str[])
{
	uint8_t i = 0;
	uint32_t temp = 0;
	for(i=0;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	*add = temp;
}
void str2int8(char *add,unsigned char str[])
{
	uint8_t i = 0;
	char temp = 0;
	if(str[0]=='-')
		i = 1;
	for(;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	if(str[0]=='-')
		temp=-temp;
	*add = temp;
}
void str2uint8(uint8_t *add,unsigned char str[])
{
	uint8_t i = 0;
	uint8_t temp = 0;
	for(i=0;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	*add = temp;
}
void str2int16(int16_t *add,unsigned char str[])
{
	uint8_t i = 0;
	int16_t temp = 0;
	if(str[0]=='-')
		i = 1;
	for(;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	if(str[0]=='-')
		temp=-temp;
	*add = temp;
}
void str2uint16(uint16_t *add,unsigned char str[])
{
	uint8_t i = 0;
	uint16_t temp = 0;
	for(i=0;str[i]!=0;i++)
	{
		temp=temp*10+str[i]-'0';
	}
	*add = temp;
}
void str2float(float *add, unsigned char str[])
{
	uint16_t point=0,j=0;
	float temp = 0.0f;
	if(str[0]=='-')
		j=1;
	for(;str[j]!=0;j++)
	{
		if(str[j]=='.')
		{
			point=10;
		}
		else if(point==0)
		{
			temp=temp*10+str[j]-'0';
		}
		else
		{
			temp+=(str[j]-'0')/(float)point;
			point=point*10;
		}
	}
	if(str[0]=='-')
		temp=-temp;
	*add = temp;
}
void float2str(float num, unsigned char str[], uint8_t scale)
{
	uint8_t i = scale;
	uint32_t temp = 0;
	if(scale >4 || scale==0)
		return;
	scale = 1;
	while(i-->0)
		scale*=10;
	i = 0;
	if(num<0)
	{
		i = 1;
		str[0] = '-';
	}
	temp = num;
	uint322str(temp,str+i);
	while(str[++i]!=0)
		;
	str[i] = '.';
	temp = (num-(float)temp)*scale;
	uint322str(temp,str+i+1);
}
void uint322str(uint32_t num,unsigned char str[])
{
	uint32_t scale = 1;
	uint8_t i = 0;
	if(num==0)
	{
		str[0] = '0';
		str[1] = 0;
		return;
	}
	while(num/scale > 9)
		scale*=10;
	while(scale>0)
	{
		str[i++] = num/scale + '0';
		num = num%scale;
		scale/=10;
	}
	str[i] = 0;
}
